package com.sh.lock;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class DeadLock {
    public static void main(String[] args) {
        new MakeUp(0, "小红").start();
        new MakeUp(1, "小明").start();

    }

}

// 口红
class Lipstick {

}

// 镜子
class Mirror {

}

class MakeUp extends Thread {

    private static Lipstick lipstick = new Lipstick();
    private static Mirror mirror = new Mirror();

    int choice;
    String name;

    MakeUp(int choice, String name) {
        this.choice = choice;
        this.name = name;
    }

    @Override
    public void run() {
        makeUp();
    }

    public void makeUp() {
        if (choice == 0) {
            synchronized (lipstick) {
                System.out.println(name + "拿到了口红");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (mirror) {
                    System.out.println(name + "拿到了镜子");
                }
            }
        } else {
            synchronized (mirror) {
                System.out.println(name + "拿到了镜子");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lipstick) {
                    System.out.println(name + "拿到了口红");
                }
            }
        }
    }
}

